"""
迭代器：实现了迭代协议的对象
    ~ __iter__ ---> 返回迭代器对象
    ~ __next__ ---> 从迭代器中取出下一个值 ---> next()
"""


class Fib:

    def __init__(self, num):
        self.a, self.b = 0, 1
        self.count = 0
        self.num = num

    def __iter__(self):
        return self

    def __next__(self):
        if self.count < self.num:
            self.a, self.b = self.b, self.a + self.b
            self.count += 1
            return self.a
        raise StopIteration()


f1 = Fib(20)
for value in f1:
    print(value)
